package com.developer.platform.controller.system;

import com.developer.platform.application.command.SystemCmdManager;
import com.developer.platform.application.command.cmd.SystemAdd;
import com.developer.platform.application.command.cmd.SystemUpdate;
import com.developer.platform.application.query.SystemQueryManager;
import com.developer.platform.application.query.dto.SystemDto;
import com.developer.platform.controller.BaseController;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2020-12-29
 */
@Api(tags = "所属系统管理")
@Validated
@RequestMapping("/system")
@RestController
public class SystemController extends BaseController {

  private final SystemCmdManager systemCmdManager;
  private final SystemQueryManager systemQueryManager;

  public SystemController(
      SystemCmdManager systemCmdManager,
      SystemQueryManager systemQueryManager) {
    this.systemCmdManager = systemCmdManager;
    this.systemQueryManager = systemQueryManager;
  }

  @Log(businessType = "所属系统管理",discription = "查询所属系统管理信息列表")
  @ApiOperation(value = "查询所属系统管理信息列表")
  @GetMapping
  public ResponseEntity<List<SystemDto>> list(){
    return ok(systemQueryManager.list());
  }

  @Log(businessType = "所属系统管理",discription = "查询所有所属系统-供条件类搜索")
  @ApiOperation(value = "查询所有所属系统")
  @GetMapping("/search")
  public ResponseEntity<List<SystemDto>> search(){
    return ok(systemQueryManager.list());
  }

  @ApiOperation(value = "查询所属系统总数")
  @GetMapping("/count")
  public ResponseEntity<Integer> count(){
    return ok(systemQueryManager.list().size());
  }

  @Log(businessType = "所属系统管理",discription = "新增所属系统信息")
  @ApiOperation(value = "新增所属系统信息")
  @PostMapping
  public ResponseEntity<?> save(@Valid @RequestBody SystemAdd systemAdd){
    systemCmdManager.save(systemAdd);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @Log(businessType = "所属系统管理",discription = "修改所属系统信息")
  @ApiOperation(value = "修改所属系统信息")
  @PutMapping("/{id}")
  public ResponseEntity<?> update(@PathVariable("id") long id ,@Valid @RequestBody SystemUpdate systemUpdate){
    systemCmdManager.update(id, systemUpdate);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  @Log(businessType = "所属系统管理",discription = "删除所属系统信息")
  @ApiOperation(value = "删除所属系统信息")
  @DeleteMapping("/{id}")
  public ResponseEntity<?> delete(@PathVariable("id") long id){
    systemCmdManager.delete(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

}
